﻿using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using SilverErp.Web.DATA;
using System.Collections.Generic;
using System;

namespace SilverErp
{
    public partial class MainPage : UserControl
    {
        ErpContext context;

        public MainPage()
        {
            InitializeComponent();
            context = new ErpContext();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            trvMenu.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(trvMenu_MouseLeftButtonUp);
        }

        void trvMenu_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            bool doubleClick = MouseButtonHelper.IsDoubleClick(sender, e);
            if (doubleClick)
            {
                TreeViewItem item = (TreeViewItem)trvMenu.SelectedItem;
                if (item != null)
                {
                    NavigateMenu((int)item.Tag);
                }
            }
        }


        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            MenuLoad();
        }

        private void MenuLoad()
        {
            EntityQuery<AnaMenu> qry = context.GetAnaMenusQuery();
            LoadOperation<AnaMenu> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<AnaMenu> op = ss as LoadOperation<AnaMenu>;
                if (op.HasError)
                {
                    ErrorWindow err = new ErrorWindow(op.Error);
                    err.Show();
                }
                else
                {
                    List<AnaMenu> menuler = op.Entities.ToList();
                    foreach (AnaMenu item in menuler)
                    {
                        TreeViewItem mn = new TreeViewItem();
                        mn.Header = item.Adi;
                        mn.Tag = item.ID;

                        if (item.ID == item.ParentID)
                        {
                            trvMenu.Items.Add(mn);
                        }
                        else
                        {
                            TreeViewItem fmn = FindMenu(item.ParentID);
                            if (fmn != null)
                            {
                                fmn.Items.Add(mn);
                            }
                        }
                    }
                }
            };
        }
        private TreeViewItem FindMenu(int id)
        {
            TreeViewItem item = null;
            for (int i = 0; i < trvMenu.Items.Count; i++)
            {
                item = FindMenu(id, (TreeViewItem)trvMenu.Items[i]);
                if (item != null)
                    break;
            }
            return item;
        }
        private TreeViewItem FindMenu(int id, TreeViewItem mn)
        {
            TreeViewItem sonuc = null;
            if (((int)mn.Tag) == id)
            {
                sonuc = mn;
            }
            else
            {
                if (mn.Items.Count > 0)
                {
                    for (int i = 0; i < mn.Items.Count; i++)
                    {
                        sonuc = FindMenu(id, (TreeViewItem)mn.Items[i]);
                    }
                }
            }
            return sonuc;
        }
        private void NavigateMenu(int id)
        {
            string url = "/Liste/";
            switch (id)
            {
                case 1001:
                    url = url + "0";
                    break;
            }

            //((Frame)(Application.Current.RootVisual as MainPage).FindName("ContentFrame")).Navigate(new Uri(url, UriKind.Relative));
            ContentFrame.Navigate(new Uri(url, UriKind.Relative));
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }
    }
}